#include "load_in_on.h"
#include<unistd.h>
// MD5哈希函数
//void md5_hash(const char *input, char *output) {
//    unsigned char digest[MD5_DIGEST_LENGTH];
//    MD5((unsigned char*)input, strlen(input), digest);
//    
//    for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
//        sprintf(&output[i*2], "%02x", digest[i]);
//    }
//    output[32] = '\0';
//}

int register_user(int sock_fd){
    //注册新用户
    char username[64];
    printf("请输入用户注册名:");
    fgets(username, sizeof(username), stdin);
    //在\n位置换成\0
    username[strcspn(username,"\n")] = '\0';
    
    char password[64];
    printf("请输入密码:");
    fgets(password, sizeof(password),stdin);
    password[strcspn(password,"\n")] = '\0';
    
    //发送注册请求
    send_tlv(sock_fd,REGISTER_REQUEST, username, strlen(username));//REGISTER_REQUEST为10
                                                                   //表示请求注册

    //接收服务器响应
    tlv_header_t header;
    if(recv_all(sock_fd,&header, sizeof(header)) <= 0){//长度不为正数接收失败
        return -1;
    }
    if(header.type == USER_EXISTS){//USER_EXISTS为11,表示用户已存在
        printf("用户名已存在,请选择其他用户名\n");
        return -1;
    }else if(header.type != SALT){//SALT为12,表示服务端注册正常
        printf("注册失败\n");
        return -1;
    }

    //接收盐值
<<<<<<< Updated upstream
    char salt[256];
=======
    char salt[33];
>>>>>>> Stashed changes
    if(recv_all(sock_fd, salt, header.length) <=0){//长度不为正数表示没有接收到盐值
        return -1;
    }
    salt[header.length]= '\0';

    //密码加盐并且MD5加密
    //char salted_inpout[128];
    //snprintf(salted_inpout, sizeof(salted_inpout),"%s%s", password, salt);
    //char hash_password[33];
    //md5_hash(salted_inpout, hash_password);  

    //发送混合加密后的密码
    char encrypted_password[1024] = {0};
<<<<<<< Updated upstream
    snprintf(encrypted_password,sizeof(encrypted_password),crypt(password,salt));
    
    send_tlv(sock_fd, REGISTER_PASSWORD, encrypted_password, 32);//REGISTER_PASSWORD为13表示是注册密码
=======
    strcat(encrypted_password, salt);
    strcat(encrypted_password, password);
    send_tlv(sock_fd, REGISTER_PASSWORD, encrypted_password, 64);//REGISTER_PASSWORD为13表示是注册密码
>>>>>>> Stashed changes

    //等待注册结果
    if(recv_all(sock_fd, &header, sizeof(header)) <= 0){
        return -1;
    }
    if(header.type == RESP_OK){ //RESP_OK为100,表示通用成功响应
        printf("注册成功\n");
        return 0;
    }
    printf("注册失败\n");
    return -1;

}

int login_user(int sock_fd){
    
    char username[64];
    printf("请输入用户登录名:");
    fgets(username, sizeof(username), stdin);
    username[strcspn(username,"\n")] = '\0';
    
    char password[64];
    printf("请输入密码:");
    fgets(password, sizeof(password),stdin);
    password[strcspn(password,"\n")] = '\0';
    
    send_tlv(sock_fd, LOGIN_REQUEST, username, strlen(username));//LOGIN_REQUEST为14表示是登陆请求
        
    //接收服务器登录响应
    tlv_header_t header;
    if(recv_all(sock_fd,&header, sizeof(header))<= 0){
        return -1;
    }
    if(header.type == USER_NOT_EXIST){
        printf("用户不存在,请先注册\n");
        return -1;
    }else if(header.type != SALT){
        printf("登录失败\n");
        return -1;
    }

    //接收盐值并返回加密密码
    char salt[33];
    if(recv_all(sock_fd, salt, header.length) <= 0){
        return -1;
    }
    salt[header.length]= '\0';
    char encrypted_password[1024] = {0};
    strcat(encrypted_password, salt);
    strcat(encrypted_password, password);
    send_tlv(sock_fd, LOGIN_PASSWORD, encrypted_password, 64);
    
    //等待登录结果
    if(recv_all(sock_fd, &header, sizeof(header)) <= 0){
        return -1;
    }
    if(header.type == LOGIN_SUCCESS){
        printf("登录成功!\n");
        return 0;
    }else if(header.type == PASSWORD_ERROR){
        printf("密码错误\n");
        return -1;
    }
    printf("登录失败\n");
    return -1;
}
